package whf.framework.i18n.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import whf.framework.config.ApplicationConfig;
import whf.framework.entity.Entity;
import whf.framework.exception.FindException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.meta.entity.IMeta;
import whf.framework.meta.entity.IProperty;
import whf.framework.meta.entity.Object;
import whf.framework.meta.entity.ObjectProperty;
import whf.framework.meta.entity.View;
import whf.framework.meta.entity.ViewProperty;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.meta.service.ViewServiceImp;
import whf.framework.util.Pair;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 * @create Apr 18, 2007 2:10:32 PM
 * 
 */
public final class MetaI18nPropertiesGenerator {
	
	/**
	 * 输出元数据属性集合
	 * @modify wanghaifeng Apr 18, 2007 2:13:41 PM
	 * @param meta
	 */
	public final static List<Pair<String, String>> outputAsProperties(Object meta){
		List<Pair<String, String>> props = Utils.newArrayList();
		Set<ObjectProperty> properties = meta.getProperties();
		for(ObjectProperty prop: properties){
			String key = meta.getBoClassName()+"."+prop.getName();
			String value = prop.getDisplayTitle();
			props.add(new Pair<String, String>(key, value));
		}
		return props;
	}
	

	/**
	 * 输出视图
	 * @modify wanghaifeng Apr 18, 2007 2:17:28 PM
	 * @param meta
	 * @return
	 */
	public final static List<Pair<String, String>> outputAsProperties(View meta){
		List<Pair<String, String>> props = Utils.newArrayList();
		Set<ViewProperty> properties = meta.getProperties();
		for(IProperty prop: properties){
			String key = "VIEW." + meta.getCode() + "."+prop.getName();
			String value = prop.getDisplayTitle();
			props.add(new Pair<String, String>(key, value));
		}
		return props;
	}
	
	/**
	 * @modify wanghaifeng Apr 18, 2007 2:19:59 PM
	 * @param meta
	 * @return
	 */
	public final static List<Pair<String, String>> outputAsProperties(IMeta meta){
		if(meta instanceof Object){
			return outputAsProperties((Object)meta);
		} else if (meta instanceof View){
			return outputAsProperties((View)meta);
		} else {
			return null;
		}
	}
	
	/**
	 * @modify wanghaifeng Apr 18, 2007 2:27:51 PM
	 * @param metas
	 * @return
	 */
	public final static List<Pair<String, String>> outputAsProperties(List<? extends IMeta> metas){
		List<Pair<String, String>> props = Utils.newArrayList();
		for(IMeta meta: metas){
			props.addAll(outputAsProperties(meta));
		}
		return props;
	}
	
	/**
	 * 输出所有对象
	 * @modify wanghaifeng Apr 18, 2007 2:31:44 PM
	 * @return
	 * @throws FindException
	 * @throws ServiceNotFoundException
	 */
	public final static List<Pair<String, String>> outputAllObjects() throws FindException, ServiceNotFoundException{
		List<Object> metas = ObjectServiceImp.getObjectService().findAllWithoutLazy();
		List<Pair<String, String>> props = Utils.newArrayList();
		for(Entity bo: metas){
			Object meta = (Object)bo;
			props.addAll(outputAsProperties(meta));
		}
		return props;
	}
	
	/**
	 * 输出所有视图
	 * @modify wanghaifeng Apr 18, 2007 2:33:16 PM
	 * @return
	 * @throws FindException
	 * @throws ServiceNotFoundException
	 */
	public final static List<Pair<String, String>> outputAllViews() throws FindException, ServiceNotFoundException{
		List<View> metas = ViewServiceImp.getViewService().findAllWithoutLazy();
		List<Pair<String, String>> props = Utils.newArrayList();
		for(Entity bo: metas){
			View meta = (View)bo;
			props.addAll(outputAsProperties(meta));
		}
		return props;
	}
	
	/**
	 * 输出所有元数据
	 * @modify wanghaifeng Apr 18, 2007 2:36:21 PM
	 * @return
	 * @throws FindException
	 * @throws ServiceNotFoundException
	 */
	public final static List<Pair<String, String>> outputAllMetas() throws FindException, ServiceNotFoundException{
		List<Pair<String, String>> props = Utils.newArrayList();
		props.addAll(outputAllObjects());
		props.addAll(outputAllViews());
		return props;
	}
	
	/**
	 * 将结果输出到文件
	 * @modify wanghaifeng Apr 18, 2007 2:52:23 PM
	 * @param properties
	 * @param file
	 * @param mode
	 * @throws IOException
	 */
	public final static void outputToFile(final List<Pair<String, String>> properties, File file, Mode mode) throws IOException{
		if(file == null) throw new IllegalArgumentException("Specify a file pls!");
		FileOutputStream fos = null;
		if(mode == Mode.APPEND){
			fos = new FileOutputStream(file, true);
		} else {
			fos = new FileOutputStream(file);
		}
		for(Pair<String, String> prop: properties){
			fos.write((prop.getText()+"="+prop.getValue()+ System.getProperty("line.separator")).getBytes());
		}
	}
	
	public final static void outputToApplicationResourceFile(final List<Pair<String, String>> properties) throws IOException{
		String i18n = ApplicationConfig.getInstance().getI18n()+"_"+Locale.getDefault()+".properties";
		String filename = ClassLoader.getSystemResource(i18n).toString().substring(6);
		File file = new File(filename);
		outputToFile(properties, file, Mode.APPEND);
	}
	
	public static enum Mode{
		NEW,
		APPEND
	}
	
	public static void main(String[] args) throws Exception{
		List<Pair<String, String>> properties = Utils.newArrayList();
		for(int i=0; i< 100; i++){
			properties.add(new Pair<String, String>("N:"+i, "V:"+i));
		}
		outputToApplicationResourceFile(properties);
	}
}
